<?php
namespace app\common\model;
use think\Db;
class User extends \think\Model{
    
    /**
     * @param unknown $openId 用户关注时，更新用户信息
     */
    public function followMp( $openid ){
        $wx   = \app\common\model\Wx::getObj();
        $info = $wx->getUserInfo( $openid );
        if($info['subscribe']){
            $d = [
                'uuid' => $openid,
                'name' => $info['nickname'],
                'sex' => $info['sex'],
                'country' => $info['country'],
                'province' => $info['province'],
                'city' => $info['city'],
                'headimgurl' => $info['headimgurl'],
                'remark' => $info['remark'],
                'active_time' => time(),
                'status' => 1,
            ];
        }else{
            $d = [
                'uuid' => $openid,
                'active_time' => time(),
                'status' => 1,
            ];
        }
        $rs   = Db::name('user')->where('uuid', $openid)->find();
        if($rs){
            $d['id'] = $rs['id'];
            Db::name('user')->update($d);
        }else{
            $d['create_time'] = time();
            Db::name('user')->insertGetId($d);
        }
        return true;
    }
    
    /**
     * 取消关注
     * @param unknown $openid
     */
    public function cancelMp( $openid ){
        $d = [
            'status' => 0,
        ];
        Db::name('user')->where('uuid', $openid)->update($d);
        return true;
    }
}